home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Data 2002 May / CD Rom Data Mayıs 2002.iso / Freeware / Blitz Basic / data1.cab / Support / help / samples / simple_game.bb < prev    next >
Encoding:
Text File  |  2002-04-10  |  2.7 KB  |  140 lines

  1. ;an example of a very - and I mean VERY - simple game!
  2. ; verified 1.48 4/18/2001
  3. ;use left and right keys to move, and space to fire
  4. ;
  5. ;hit ESC to exit
  6.  
  7. ;go into graphics mode
  8. Graphics 640,480
  9.  
  10. ;enable double buffering
  11. SetBuffer BackBuffer()
  12.  
  13. ;load images for player, bullet and target
  14. player=LoadImage("graphics\player.bmp")
  15. bullet=LoadImage("graphics\bullet.bmp")
  16. target=LoadImage("graphics\alien.bmp" )
  17.  
  18. ;initialize player position
  19. player_x=320
  20. player_y=440
  21.  
  22. ;initialize target position and direction of movement
  23. target_x=0
  24. target_y=20
  25. target_direction=1
  26.  
  27. ;initialize bullet position and 'fired' flag
  28. bullet_x=0
  29. bullet_y=0
  30. bullet_fired=False
  31.  
  32. ;initialize player score
  33. score=0
  34.  
  35. ;loop until ESC hit...
  36. While Not KeyDown(1)
  37.  
  38.     ;is left key being held?
  39.     If KeyDown(203)
  40.     
  41.         ;move player to the left
  42.         player_x=player_x-4
  43.         
  44.         ;stop the player going 'off screen'
  45.         If player_x<0 Then player_x=0
  46.         
  47.     EndIf
  48.     
  49.     ;is right key being held?
  50.     If KeyDown(205)
  51.     
  52.         ;move player to the right
  53.         player_x=player_x+4
  54.         
  55.         ;stop the player going 'off screen'
  56.         If player_x>604 Then player_x=604
  57.     
  58.     EndIf
  59.     
  60.     ;has the 'fire' key been hit?
  61.     If KeyHit(57)
  62.     
  63.         ;initialize bullet position
  64.         bullet_x=player_x+9
  65.         bullet_y=player_y-10
  66.         
  67.         ;enable the bullet
  68.         bullet_fired=True
  69.     
  70.     EndIf
  71.     
  72.     ;update the target
  73.     target_x=target_x+target_direction
  74.     
  75.     ;if target hits the edge of the screen, make it bounce
  76.     If target_x<0 Or target_x>590
  77.         
  78.         ;reverse the direction target is moving in
  79.         target_direction=-target_direction
  80.         
  81.     EndIf
  82.     
  83.     ;update the bullet, if it exists
  84.     If bullet_fired
  85.     
  86.         ;move the bullet up
  87.         bullet_y=bullet_y-4
  88.         
  89.         ;if the bullet is still onscreen, see if it hits the target
  90.         If bullet_y>0
  91.         
  92.             ;does the bullet overlap the target?
  93.             If ImagesCollide( bullet,bullet_x,bullet_y,0,target,target_x,target_y,0 )
  94.             
  95.                 ;yes! the player shot the target! increase the score
  96.                 score=score+100
  97.                 
  98.                 ;reset the target
  99.                 target_x=0
  100.                 target_direction=Abs(target_direction)
  101.                 
  102.                 ;increase the speed of the target a little!
  103.                 target_direction=target_direction+1
  104.                 
  105.                 ;and turn the bullet off
  106.                 bullet_fired=False
  107.                 
  108.             EndIf
  109.         
  110.         Else
  111.         
  112.             ;bullet has gone offscreen - reduce the score
  113.             score=score-20
  114.         
  115.             ;and disable the bullet
  116.             bullet_fired=False
  117.             
  118.         EndIf
  119.         
  120.     EndIf
  121.     
  122.     ;clear the screen
  123.     Cls
  124.     
  125.     ;draw the target
  126.     DrawImage target,target_x,target_y
  127.     
  128.     ;draw the bullet, if it exists
  129.     If bullet_fired Then DrawImage bullet,bullet_x,bullet_y
  130.     
  131.     ;draw the player
  132.     DrawImage player,player_x,player_y
  133.     
  134.     ;show the score
  135.     Text 320,0,score,1
  136.     
  137.     ;swap front and back buffers
  138.     Flip
  139.     
  140. Wend